package com.sise.controller;

import com.sise.common.dto.OrderDTO;
import com.sise.common.interfaces.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author idea
 * @data 2019/10/7
 */
@RestController
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping(value = "/createOrder")
    public ResponseEntity<String> createOrder(int orderNum) {
        for (int i = 0; i < orderNum; i++) {
            orderService.createOrder(buildRandOrderDTO());
        }
        return ResponseEntity.ok("插入完毕");
    }

    @PostMapping(value = "/updateOrder")
    public ResponseEntity<String> updateOrder(String orderNum){
        OrderDTO orderDTO=new OrderDTO();
        orderDTO.setOrderNumber(orderNum);
        orderDTO.setOrderStatus(1);
        Boolean updateStatus = orderService.updateExpireOrder(orderDTO);
        return ResponseEntity.ok("更新完毕"+updateStatus);
    }

    /**
     * 构建随机订单
     *
     * @return
     */
    private OrderDTO buildRandOrderDTO() {
        ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
        Integer randNumber = threadLocalRandom.nextInt(10000000);
        Integer buyerId = threadLocalRandom.nextInt(10000000);
        Integer outerOrderNo = threadLocalRandom.nextInt(10000000);
        OrderDTO orderDTO = OrderDTO.builder()
                .orderNumber("mall-" + randNumber)
                .buyerId(buyerId)
                .orderStatus(0)
                .outerOrderNo(outerOrderNo)
                //默认未支付
                .payStatus(0)
                .price(new BigDecimal(1000))
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        return orderDTO;
    }
}
